import { _decorator, Button, Component, Enum, instantiate, Label, Node, Prefab, resources, Tween, tween, Vec3 } from 'cc';
import PropertySet from '../PropertySet';
import { LogMgr } from '../../../../framework/log/LogMgr';
import { Hero } from '../../hero/Hero';
import AssetMgr from '../../../../framework/asset/AssetMgr';
import { ResourceNames } from '../../../constant/CommonConstant';
import EventMgr from '../../../../framework/event/EventMgr';
import { NormalMonster } from '../../monster/NormalMonster';
const { ccclass, property } = _decorator;

@ccclass('ZhanyiBattle')
export class ZhanyiBattle extends Component {
    startNumber = 6;

    @property({type:Label,displayName:"倒计时节点"})
    startTimeLable:Label;

    @property({type:Node,displayName:"敌人缓存节点"})
    enemyCacheNode:Node;  
    @property({type:Node,displayName:"怪物出生点"})
    enemyBornPosPNode:Node;  
    roadIndex:number = 0;
    @property({type:Node,displayName:"怪物路店"})
    enemyRoads:Node[] = []
    

    @property({type:Node,displayName:"英雄放置位置"})
    heroSetNode:Node[] = [];

    @property({type:Node,displayName:"卡牌展示位置父节点"})
    heroCardSetPNode:Node;
    heroSets:Node[] = []

    heroPrefab:Prefab = null;
    enimyPrefab:Prefab = null;

    battleEvtMgr:EventMgr = new EventMgr();

    @property({type:Button,displayName:"抽牌按钮"})
    drawBtn:Button;

    @property({type:Node,displayName:"抽牌面板"})
    drawPopNode:Node = null;

    @property({type:Button,displayName:"抽牌面板Mask"})
    drawPopMask:Button = null;

    /**
     * 所有敌人
     */
    enemys:Hero[] = []
    /**
     * 所有英雄
     */
    heros:Hero[] = []
    running:boolean = false;
    lastEnemyBorn = 0;

    enemyBornd:boolean = false;


    //TODO 卡池


    start() {

        this.initHerosPos();
        let testHeroPrefab = AssetMgr.Ins.loadPrefab(ResourceNames.PrefabResources,"battle/hero/battle_hero")
        testHeroPrefab.then((p)=>{
            this.heroPrefab = p;
            this.addHero(1,this.heroSets[0])
        })
        let testEnemyPrefab = AssetMgr.Ins.loadPrefab(ResourceNames.PrefabResources,"battle/enimy/enimy_1")
        testEnemyPrefab.then((p)=>{
            this.enimyPrefab = p;
        })
        this.addListener();
        this.startAni()
        

    }
    addListener() {
        this.battleEvtMgr.on("startBattle",this.battleStart,this)
        this.drawBtn.node.on(Button.EventType.CLICK,this.drawCard,this)
        this.drawPopMask.node.on(Button.EventType.CLICK,this.clickDrawMask,this)
    }

    //============================[ 战斗相关事件_Start ]====================================
    //开始战斗
    public battleStart() {
        LogMgr.debug("战斗开始")
        //开始生成敌人,根据配置生成怪物
        this.running = true;
    }
    //战斗暂停
    public battlePause() {
        LogMgr.debug("战斗暂停")
    }
    //战斗加速
    public battleAddSpeed() {
        LogMgr.debug("战斗暂停")
    }

    //战斗结束
    public battleEnd() {
        LogMgr.debug("战斗结束")
    }
    // 抽卡
    public drawCard() {
        if(this.drawPopNode.active){
            //面板是打开状态，则从卡池中抽卡
            LogMgr.debug("抽卡面板已经打开,再次点击，从卡池抽卡")
            this.drawCardFromPool()
            return
        }else{
            this.drawPopNode.active = true;
            //如果上次抽取的卡牌还在，则不再抽取
        }

    }
    drawCardFromPool() {

    }
    public clickDrawMask() {
        this.drawPopNode.active = false
    }
    // 升级人口
    // 挑战怪物（材料怪物,装备怪物）
    //============================[ 战斗相关事件_End ]====================================

    public addHero(heroId:number,heroPNode:Node){
        let heroNode =instantiate(this.heroPrefab)
        heroNode.setPosition(new Vec3(0,0,0))
        heroNode.setParent(this.heroSets[0])
        let hero = heroNode.addComponent(Hero)
        this.heros.push(hero)
        hero.id = 1;

    }
    initHerosPos() {
        if(!this.heroCardSetPNode){
            LogMgr.err("英雄位置父节点未赋值，无法放置影响")
        }
        this.heroCardSetPNode.children.forEach(n=>this.heroSets.push(n))
        if(this.heroSets.length==0){
            LogMgr.err("英雄放置点为空，无法放置英雄！")
        }
    }

    startAni(){
        LogMgr.debug("开始倒计时！")
        // this.schedule(this.startAniDo,1,6,2)
        this.startAniDo()
    }
    startAniDo(){
        LogMgr.debug("执行了倒计时动画")
        let aniTween = tween(this.startTimeLable.node)
            .to(0.7,{scale:new Vec3(1,1,0)},{
                easing:"backIn"
            })
            .delay(0.2)
            .call(()=>{
                this.startTimeLable.node.scale = new Vec3(0,0,0);
                this.startNumber--;
                this.startTimeLable.string = this.startNumber+"";
                if(this.startNumber==0){
                    this.battleEvtMgr.emmit("startBattle") 
                }
            })
        tween(this.startTimeLable.node).repeat(6,aniTween).start()
    }



    update(deltaTime: number) {
        if(!this.running){
            return
        }
        //生成敌人
        if(!this.enemyBornd){
            this.enimyBorn()
            this.enemyBornd = true;
        }

        // if(this.lastEnemyBorn>3){
        //     this.lastEnemyBorn = 0;
        //     this.enimyBorn()

        // }else{
        //     this.lastEnemyBorn+=deltaTime;
        // }
    }
    enimyBorn() {
        let enimyNode =instantiate(this.enimyPrefab)
        let pos = this.enemyBornPosPNode.position
        enimyNode.setPosition(new Vec3(pos.x,pos.y,pos.z))
        enimyNode.setParent(this.enemyCacheNode)
        let monster = enimyNode.addComponent(NormalMonster)
        monster.roadNodes = this.enemyRoads;
        // this.enemys.push(hero)
    }
}




